<template>
	<cl-popup v-model="visible" direction="bottom" size="60%" @close="handleClose">
		<view class="spec-selector">
			<!-- 商品信息头部 -->
			<view class="goods-header">
				<image
					:src="goods.mainPic || '/static/empty/no.jpg'"
					class="goods-image"
					mode="aspectFit"
				/>
				<view class="goods-info">
					<view class="goods-name">{{ goods.title }}</view>
					<view class="goods-price">
						<text class="price-symbol">¥</text>
						<text class="price-value">{{ formatPrice(goods.price) }}</text>
						<text class="price-unit">/ {{ goods.unit }}</text>
					</view>
					<view class="goods-stock">
						库存：{{ goods.storNum || 0 }}{{ goods.unit }}
					</view>
				</view>
				<view class="close-btn" @tap="handleClose">
					<cl-icon name="close" size="20" color="#999" />
				</view>
			</view>

			<!-- 商品规格信息 -->
			<view class="spec-section" v-if="goods.goodsSpec">
				<view class="section-title">规格</view>
				<view class="spec-info">
					<text class="spec-text">{{ goods.goodsSpec }}</text>
				</view>
			</view>

			<!-- 包装规格选择 -->
			<view class="package-section" v-if="goods.midpack > 1">
				<view class="section-title">包装规格</view>
				<view class="package-options">
					<view
						class="package-option"
						:class="{ active: selectedPackage === 'midpack' }"
						@tap="selectPackage('midpack')"
					>
						<view class="option-name">中包装</view>
						<view class="option-desc">{{ goods.midpack }}{{ goods.unit }}</view>
					</view>
					<view
						class="package-option"
						:class="{
							active: selectedPackage === 'single',
							disabled: goods.isMidpackSaleDisabled === 1,
						}"
						@tap="selectPackage('single')"
						v-if="goods.isMidpackSaleDisabled === 0"
					>
						<view class="option-name">单包装</view>
						<view class="option-desc">1{{ goods.unit }}</view>
					</view>
				</view>
			</view>

			<!-- 数量选择 -->
			<view class="quantity-section">
				<view class="section-title">数量</view>
				<view class="quantity-selector">
					<cl-input-number
						v-model="quantity"
						:min="1"
						:max="maxQuantity"
						:step="1"
						size="large"
					/>
					<text class="quantity-unit">{{ currentUnit }}</text>
				</view>
			</view>

			<!-- 处方药提示 -->
			<view class="prescription-notice" v-if="goods.isPres === 1">
				<cl-icon name="info-circle" size="16" color="#ff9500" />
				<text class="notice-text">此商品为处方药，需要医师处方才能购买</text>
			</view>

			<!-- 底部操作按钮 -->
			<view class="action-buttons">
				<cl-button
					type="primary"
					size="large"
					@tap="handleConfirm"
					:disabled="!canConfirm"
					class="confirm-btn"
				>
					{{ mode === "cart" ? "加入购物车" : "立即购买" }}
				</cl-button>
			</view>
		</view>
	</cl-popup>
</template>

<script lang="ts" setup>
import { ref, computed, watch } from "vue";

interface Props {
	show: boolean;
	goods: any;
	mode: "cart" | "buy";
}

interface Emits {
	(e: "close"): void;
	(e: "confirm", spec: { count: number; packageType: string }): void;
}

const props = withDefaults(defineProps<Props>(), {
	show: false,
	goods: () => ({}),
	mode: "cart",
});

const emit = defineEmits<Emits>();

const visible = ref(false);
const quantity = ref(1);
const selectedPackage = ref("midpack");

// 计算属性
const maxQuantity = computed(() => {
	const stock = Number(props.goods.storNum || 0);
	if (selectedPackage.value === "midpack" && props.goods.midpack > 1) {
		return Math.floor(stock / props.goods.midpack);
	}
	return stock;
});

const currentUnit = computed(() => {
	if (selectedPackage.value === "midpack" && props.goods.midpack > 1) {
		return `${props.goods.midpack}${props.goods.unit}装`;
	}
	return props.goods.unit || "件";
});

const canConfirm = computed(() => {
	return quantity.value > 0 && quantity.value <= maxQuantity.value;
});

// 监听show属性变化
watch(
	() => props.show,
	(newVal) => {
		visible.value = newVal;
		if (newVal) {
			// 重置数据
			quantity.value = 1;
			selectedPackage.value = "midpack";
		}
	},
);

// 监听visible变化
watch(visible, (newVal) => {
	if (!newVal) {
		emit("close");
	}
});

// 方法
function formatPrice(price: any): string {
	const numPrice = Number(price);
	if (!numPrice || numPrice === 0 || isNaN(numPrice)) return "暂无报价";
	return numPrice.toFixed(2);
}

function selectPackage(type: string) {
	if (type === "single" && props.goods.isMidpackSaleDisabled === 1) {
		return;
	}
	selectedPackage.value = type;
	quantity.value = 1; // 重置数量
}

function handleClose() {
	visible.value = false;
}

function handleConfirm() {
	if (!canConfirm.value) {
		uni.showToast({ title: "请选择正确的数量", icon: "none" });
		return;
	}

	const actualCount =
		selectedPackage.value === "midpack" && props.goods.midpack > 1
			? quantity.value * props.goods.midpack
			: quantity.value;

	emit("confirm", {
		count: actualCount,
		packageType: selectedPackage.value,
	});
}
</script>

<style lang="scss" scoped>
.spec-selector {
	padding: 0 32rpx 32rpx;
}

.goods-header {
	display: flex;
	align-items: center;
	padding: 32rpx 0;
	border-bottom: 1rpx solid #f0f0f0;
	position: relative;
}

.goods-image {
	width: 120rpx;
	height: 120rpx;
	border-radius: 12rpx;
	background-color: #f5f5f5;
}

.goods-info {
	flex: 1;
	margin-left: 24rpx;
}

.goods-name {
	font-size: 28rpx;
	color: #333;
	line-height: 1.4;
	margin-bottom: 12rpx;
}

.goods-price {
	display: flex;
	align-items: baseline;
	margin-bottom: 8rpx;
}

.price-symbol {
	font-size: 24rpx;
	color: #ff4757;
}

.price-value {
	font-size: 32rpx;
	font-weight: bold;
	color: #ff4757;
	margin: 0 4rpx;
}

.price-unit {
	font-size: 24rpx;
	color: #999;
}

.goods-stock {
	font-size: 24rpx;
	color: #666;
}

.close-btn {
	position: absolute;
	top: 32rpx;
	right: 0;
	padding: 8rpx;
}

.spec-section,
.package-section,
.quantity-section {
	margin-top: 40rpx;
}

.section-title {
	font-size: 28rpx;
	font-weight: 600;
	color: #333;
	margin-bottom: 24rpx;
}

.spec-info {
	padding: 24rpx;
	background-color: #f8f9fa;
	border-radius: 12rpx;
}

.spec-text {
	font-size: 26rpx;
	color: #666;
}

.package-options {
	display: flex;
	gap: 24rpx;
}

.package-option {
	flex: 1;
	padding: 24rpx;
	border: 2rpx solid #e0e0e0;
	border-radius: 12rpx;
	text-align: center;
	transition: all 0.3s ease;

	&.active {
		border-color: #667eea;
		background-color: #f8f9ff;
	}

	&.disabled {
		opacity: 0.5;
		pointer-events: none;
	}
}

.option-name {
	font-size: 26rpx;
	color: #333;
	margin-bottom: 8rpx;
}

.option-desc {
	font-size: 24rpx;
	color: #666;
}

.quantity-selector {
	display: flex;
	align-items: center;
	gap: 16rpx;
}

.quantity-unit {
	font-size: 26rpx;
	color: #666;
}

.prescription-notice {
	display: flex;
	align-items: center;
	gap: 12rpx;
	margin-top: 32rpx;
	padding: 24rpx;
	background-color: #fff7e6;
	border-radius: 12rpx;
}

.notice-text {
	font-size: 24rpx;
	color: #ff9500;
	line-height: 1.4;
}

.action-buttons {
	margin-top: 48rpx;
	padding-top: 32rpx;
	border-top: 1rpx solid #f0f0f0;
}

.confirm-btn {
	width: 100%;
}
</style>
